import { ChatMessage } from "prismarine-chat";
import { GameChatMsgType } from "../type/gameType.js";

// role 决定了消息的来源，是玩家还是系统
function _roleRouter(jsonMsg: ChatMessage): "system" | "player" {
  if (
    jsonMsg.translate === "chat.type.text" ||
    jsonMsg.translate === "commands.message.display.incoming"
  ) {
    return "player";
  } else {
    return "system";
  }
}

// 递归查找命令错误标志
function _findCommandErrorFlag(jsonMsg: any): boolean {
  if (!jsonMsg || typeof jsonMsg !== "object") {
    return false;
  }
  if ("translate" in jsonMsg && typeof jsonMsg.translate === "string") {
    return jsonMsg.translate === "command.context.here";
  }
  for (const key in jsonMsg) {
    const found = _findCommandErrorFlag(jsonMsg[key]);
    if (found) {
      return found;
    }
  }

  return false;
}

// type 决定了消息的具体类型，是聊天、私聊还是状态反馈等
function _typeRouter(
  jsonMsg: ChatMessage
): "chat" | "whisper" | "state" | "command" | "error" | null {
  if (_findCommandErrorFlag(jsonMsg)) return "error";
  switch (jsonMsg.translate) {
    case "chat.type.text":
      return "chat";
    case "commands.message.display.incoming":
      return "whisper";
    default:
      if (jsonMsg.translate) {
        if (
          jsonMsg.translate.startsWith("chat.type.advancement") ||
          jsonMsg.translate.startsWith("death")
        )
          return "state";
        if (
          jsonMsg.translate.startsWith("commands") ||
          jsonMsg.translate.startsWith("chat.type") ||
          jsonMsg.translate.startsWith("multiplayer.player")
        ) {
          return "command";
        }
        if (jsonMsg.translate.startsWith("multiplayer.disconnect")) return "error";
      }
  }
  return null;
}

// 解析玩家名称
// <PlayerName> message
// PlayerName whispers to you: message
function _parsePlayerName(content: string): string | null {
  const chatMatch = content.match(/^<([^>]+)>/);
  const whisperMatch = content.match(/^([^ ]+) whispers to you:/);
  if (chatMatch && chatMatch[1]) return chatMatch[1];
  if (whisperMatch && whisperMatch[1]) return whisperMatch[1];
  return null;
}

// 解析消息
// <PlayerName> message
// PlayerName whispers to you: message
function _parseMessage(content: string): string {
  const chatMatch = content.match(/^<[^>]+>\s*(.*)/);
  const whisperMatch = content.match(/^[^ ]+ whispers to you:\s*(.*)/);
  if (chatMatch && chatMatch[1]) {
    return chatMatch[1].trim();
  }
  if (whisperMatch && whisperMatch[1]) {
    return whisperMatch[1].trim();
  }

  return "";
}

// 聊天消息路由
export function chatMsgRouter(jsonMsg: ChatMessage): GameChatMsgType | null {
  const type = _typeRouter(jsonMsg);
  if (!type) return null;
  let result: GameChatMsgType = { type: "chat", role: "player", content: "", name: "" };
  const content: string = jsonMsg.toString();
  const role: "system" | "player" = _roleRouter(jsonMsg);
  const playerName: string | null = _parsePlayerName(content);
  result.type = type;
  result.role = role;
  result.content = content;
  result.name = playerName;
  if (type === "whisper" || type === "chat") {
    result.message = _parseMessage(content);
  }
  return result;
}

// undefined 可能是命令错误
// chat.type.text - 聊天
// chat.type.admin - 管理员信息
// chat.type.advancement.* - 成就
// commands.message.display.incoming - 私聊收到
// commands.xx.xx - 命令反馈
// multiplayer - 多人游戏
// commands.xx.xx - 命令反馈
// death.xxx 死亡消息
